package com.gongpingjialoan.activity.car;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.category.PinnedHeaderListView;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.car.CarModelDetailAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/9/28.
 */
public class CarEvalActivity extends BaseActivity implements AdapterView.OnItemClickListener, NetDataJson.OnNetDataJsonListener {

    private String car_id, car_year;

    private PinnedHeaderListView mListView;

    private NetDataJson netDataJson;

    private NetDataJson evalData;

    private LoadingDialog loadingDialog;

    private CarModelDetailAdapter adapter;

    private TextView textView;
    private ImageView nodataimageView;
    private JSONArray jsonArray = null;
    private String new_car_price;
    private String mBrandSlug, mModelSlug, sourceType, year, mile, model_deail_slug, city;

    String s4_price_range;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        isScrollPage = true;
        action = "detail_eval";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.car_eval);
        setTitle("选择具体款型");
        loadingDialog = new LoadingDialog(this, "加载中...");
        mListView = (PinnedHeaderListView) this.findViewById(R.id.listview);
        textView = (TextView) this.findViewById(R.id.title);
        nodataimageView = (ImageView) this.findViewById(R.id.no_info);
        adapter = new CarModelDetailAdapter(this);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(this);
        car_year = getIntent().getStringExtra("year");
        car_id = getIntent().getStringExtra("car_id");
        mBrandSlug = getIntent().getStringExtra("brand_slug");
        mModelSlug = getIntent().getStringExtra("model_slug");
        sourceType = getIntent().getStringExtra("source_type");
        mile = getIntent().getStringExtra("mile");
        city = getIntent().getStringExtra("city");
        textView.setText(getIntent().getStringExtra("title"));
        netDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                loadingDialog.dismiss();
                Toast.makeText(CarEvalActivity.this, errorMessage, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                loadingDialog.dismiss();
                try {
                    jsonArray = json.getJSONArray("model_details");
                    if (jsonArray.length() > 0) {
                        adapter.setData(jsonArray);
                    } else {
                        nodataimageView.setVisibility(View.VISIBLE);
                    }
                } catch (Exception e) {

                }


            }
        });
        netDataJson.setUrl(API.eval_cars);
        netDataJson.addParam("car_id", car_id);
        netDataJson.request("get");
        loadingDialog.show();
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        try {
            JSONObject obj = jsonArray.getJSONObject(i);
            loadingDialog.show();
            evalData = new NetDataJson(this);
            evalData.addParam("brand", mBrandSlug);
            evalData.addParam("model", mModelSlug);
            evalData.addParam("mile", mile);
            evalData.addParam("city", city);
            evalData.addParam("source_type", sourceType);
            evalData.addParam("year", car_year);
            new_car_price = obj.getString("price_bn");
            s4_price_range = obj.getString("4s_price_range").toString();
            model_deail_slug = obj.getString("detail_model_slug");
            evalData.addParam("model_detail_slug", model_deail_slug);
            evalData.setUrl(API.eval_cars_detail);
            evalData.request("get");
        } catch (JSONException e) {
        }

    }

    @Override
    public void onDataJsonError(String errorMessage) {
        loadingDialog.dismiss();
        Toast.makeText(CarEvalActivity.this, errorMessage, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDataJsonUpdate(JSONObject json) {
        loadingDialog.dismiss();
        try {
            Intent intent = new Intent();
            intent.putExtra("eval_price", json.getDouble("evaluate_price"));
            intent.putExtra("new_car_price", new_car_price);
            intent.putExtra("model_deail_slug", model_deail_slug);
            intent.putExtra("4s_price_range", s4_price_range);
            setResult(RESULT_OK, intent);
            finish();
        } catch (JSONException e) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (evalData != null) {
            evalData.cancelTask();
        }
        if (netDataJson != null) {
            netDataJson.cancelTask();
        }
    }
}
